package com.hunan.api.catv.action.live;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.annotation.MemberAuth;
import com.hunan.api.catv.common.Api;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.live.LiveBiz;
import com.hunan.api.catv.service.biz.tencent_cloud.im.ImBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(Api.live)
@Validated
public class LiveAction extends BaseAction {

    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    ImBiz imBiz;

    @Autowired
    LiveBiz liveBiz;

    @GetMapping("/list")
    public Result list(Integer columnId, Integer contentType, Integer liveStatus, Integer pageNum, HttpServletRequest request) {
//        Integer memberId = getMemberIdV2(request);
       /* if (memberId != null && contentType != null && ColumnEnum.tv.getCode().equals(contentType)) {
            return buildOK(liveBiz.apiPage(columnId, contentType, liveStatus, pageNum, getPageSize(request), memberId), "");
        } else {*/
        return buildOK(cacheBiz.livePage(columnId, contentType, liveStatus, pageNum, getPageSize(request)), "");
//        }
    }

    @GetMapping("/top")
    public Result top(Integer columnId, Integer contentType, Integer liveStatus, HttpServletRequest request) {
        return buildOK(cacheBiz.liveTopPage(columnId, contentType, liveStatus), "");
    }

    @GetMapping("/info")
    public Result info(Integer liveId) {
        if (ObjectUtils.isEmpty(liveId)) {
            return buildRequire("liveId require");
        }
        return buildOK(cacheBiz.liveInfo(liveId), "");
    }

    @GetMapping("/getLiveInfo/{liveId}")
    public Result getLiveInfo(@PathVariable("liveId") Integer liveId) {
        if (ObjectUtils.isEmpty(liveId)) {
            return buildRequire("liveId require");
        }
        return buildOK(cacheBiz.getLiveInfo(liveId), "");
    }

    //@GetMapping("/playAddress")
    public Result playAddress(HttpServletRequest request, Integer type, String channelId, String startTime, String endTime) {
        if (ObjectUtils.isEmpty(channelId)) {
            return buildRequire("channelId require");
        }
        return buildResult(liveBiz.playAddress(type, channelId, getRemoteIp(request), startTime, endTime, getDeviceId(request)));
    }

    //@PostMapping("/p")
    public Result p(HttpServletRequest request, Integer type, String channelId, String startTime, String endTime) {
        if (ObjectUtils.isEmpty(channelId)) {
            return buildRequire("require");
        }
        if (ObjectUtils.isEmpty(getDeviceId(request))) {
            return buildRequire("require");
        }
        return buildResult(liveBiz.p(type, channelId, getRemoteIp(request), startTime, endTime, getDeviceId(request)));
    }

    //@PostMapping("/playAdr")
    public Result playAdr(HttpServletRequest request, Integer type, String channelId, String startTime, String endTime) {
        if (ObjectUtils.isEmpty(channelId)) {
            return buildRequire("require");
        }
        if (ObjectUtils.isEmpty(getDeviceId(request))) {
            return buildRequire("require");
        }
        return buildResult(liveBiz.playAdr(type, channelId, getRemoteIp(request), startTime, endTime, getDeviceId(request)));
    }

    @PostMapping("/playAdrV2")
    public Result playAdrV2(HttpServletRequest request, Integer type, String channelId, String startTime, String endTime) {
        if (ObjectUtils.isEmpty(channelId)) {
            return buildRequire("require");
        }
        if (ObjectUtils.isEmpty(getDeviceId(request))) {
            return buildRequire("require");
        }
        return buildResult(liveBiz.playAdrV2(type, channelId, getRemoteIp(request), startTime, endTime, getDeviceId(request)));
    }

    @GetMapping("/week")
    public Result week() {
        return buildOK(cacheBiz.scheduleWeek("key"), "");
    }

    @GetMapping("/schedulePage")
    public Result schedulePage(String channelId, String time, Integer liveStatus, Integer pageNum, HttpServletRequest request) {
        return buildOK(cacheBiz.schedulePage(pageNum, 100, channelId, time), "");
    }

    @MemberAuth
    @GetMapping("/sign")
    public Result sign(HttpServletRequest request) {
        return buildResult(imBiz.getSign(2, getMemberId(request)));
    }

    @GetMapping("/xw")
    public Result xw() {
        return buildResult(liveBiz.xwRecommend());
    }

    @MemberAuth
    @GetMapping("/exclusiveAll")
    public Result exclusiveAll(HttpServletRequest request) {
        return buildResult(liveBiz.exclusiveAll(getMemberId(request)));
    }

    @MemberAuth
    @GetMapping("/exclusiveInfo")
    public Result exclusiveInfo(HttpServletRequest request, String title) {
        return buildResult(liveBiz.exclusiveInfo(getMemberId(request), title));
    }

    @MemberAuth
    @PostMapping("/exclusiveSave")
    public Result exclusiveSave(HttpServletRequest request, String title, String array) {
        return buildResult(liveBiz.exclusiveSave(getMemberId(request), title, array));
    }

    @MemberAuth
    @PostMapping("/exclusiveEdit")
    public Result exclusiveEdit(HttpServletRequest request, String preTitle, String title) {
        return buildResult(liveBiz.exclusiveEdit(getMemberId(request), preTitle, title));
    }

    @MemberAuth
    @GetMapping("/newYearParty/{pwd}")
    public Result newYearParty(@PathVariable String pwd, HttpServletRequest request) {
        return buildResult(liveBiz.newYearParty(pwd, getRemoteIp(request)));
    }

}
